// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import gameData from "../gameData";

const {ccclass, property} = cc._decorator;

let tipList = [
    ["你…你是BUG君!","可恶!他们居然把你封印在这种绝境里!"],
    ["我是BUG的制造者,这是我应得的惩罚!","勇者大人,你离开吧,让我自生自灭…"],
    ["不!你应该站出来,去修复勇者世界的错误!","我们要一起打败大魔王!救出宠物们!"],
    ["你说的对,我不应该逃避","可是熄灭火焰的唯一方法,只有…"],
    ["火焰禁制打开了!","可是BUG君,我们离得太远了…"],
    ["还差最后一步","你要激活石阶","才能到我这里来"]
];

let List1 = ["......","大魔王最近太猖狂，连圣诞老人都不放过","我得去调查一下，看看究竟是什么情况"];
let List2 = [
    ["咕咕鸡，你怎么在这里？","你有见到过大魔王和小鹿吗？"],
    ["咕叽！"],
    ["什么，他们在哪里？"],
    ["咕咕咕咕咕叽叽叽叽！"],
    ["居然是这样，没想到大魔王如此险恶用心!","你快回去吧，这里交给我就好了!"],
    ["咕咕叽！咕咕叽叽！"]
];
let List3 = [
    ["哦嚯嚯，梅花十三，你终于来了","本大王引你来这里，就是为了让你欣赏下，本大王今年收到的圣诞礼物~","就连使者小鹿，也被我的魅力折服，自愿成为本大王的宠物呢~"],
    ["别胡说八道了，这是圣诞老人给勇者们的礼物","你根本就是抢走的，还抓走了送礼物的小鹿","你说是你引我来？难道…你又假扮咕咕鸡骗人？"],
    ["……这不重要！","本大王知道，圣诞老头派你来抢走本大王的礼物。","本大王也不为难你，你只要能破解一个简单的谜题，这些礼物就送给你。怎么样？"],
    ["哼，你有这么好心？","算了，说吧，什么谜题。"],
    ["本大王很忙，你自己看吧，解开了我再回来把礼物给你。"],
];

let List4 = [
    ["天天吵得我睡不着…原来是要用红宝石安抚那家伙才行…"],
    ["……你说什么？"],
    ["嗯？咳咳…我是说你看起来笨，其实只比我差那么一点点"],
    ["……把礼物和小鹿还给我"],
    ["哼，看在你这次诚心诚意的份上，就把礼物给你吧。","本大王和你这种无聊勇者不一样，还要赶去参加好几个圣诞聚会，先走了，下次见。"],
    ["骗人吧，真的会有人邀请大魔王参加圣诞聚会吗…","我可是再也不想和你“下次见”。"],
];

@ccclass
export default class conversationWindow extends cc.Component {

    @property(cc.Label)
    labList : cc.Label[] = [];

    @property(cc.Label)
    roleLab : cc.Label = null;

    @property(sp.Skeleton)
    roleSkin : sp.Skeleton = null;

    @property(sp.Skeleton)
    otherSkin : sp.Skeleton = null;

    @property()
    showTimes = 2;

    callback = null;

    curOrder = 0;

    onLoad () {
        let curSkin = -1;
        if(gameData.randomSkinIndex != -1){
            curSkin = gameData.randomSkinIndex;
        }else{
            curSkin = gameData.getCurUseRoleSkin();
        }
        this.roleLab.string = gameData.getRoleShopData()[curSkin]["rsname"];
        if(curSkin < 9){
            cc.loader.loadRes("anim/zhujue",sp.SkeletonData,(err,res)=>{
                if(err){
                    console.log(err);
                    return
                }
                this.roleSkin.skeletonData = res;
                this.roleSkin.setSkin("pf0" +  (curSkin + 1));
                this.roleSkin.setAnimation(0,"animation",true);
            });
        }else{
            if(curSkin >= 12){
                cc.loader.loadRes("anim/zhujue" + (curSkin + 2),sp.SkeletonData,(err,res)=>{
                    if(err){
                        console.log(err);
                        return
                    }
                    this.roleSkin.skeletonData = res;
                    if(curSkin == 12){
                        this.roleSkin.setSkin("pfi" + (gameData.lastColorfulPart + 1));
                    }else if(curSkin == 14){
                        this.roleSkin.setSkin("pf1");
                    }else if(curSkin == 16){
                        this.roleSkin.setSkin("pifu" + (gameData.lastColorfulList[1] + 1));
                    }else if(curSkin == 17){
                        this.roleSkin.setSkin("pf01");
                    }
                    this.roleSkin.setAnimation(0,"animation",true);
                })
            }else{
                cc.loader.loadRes("anim/zhujue",sp.SkeletonData,(err,res)=>{
                    if(err){
                        console.log(err);
                        return
                    }
                    this.roleSkin.skeletonData = res;
                    this.roleSkin.setSkin("pf" +  (curSkin + 1));
                    this.roleSkin.setAnimation(0,"animation",true);
                });
            }
        }
    }

    updateConversation(actionOrder,callback){
        if(actionOrder > this.showTimes) return;
        switch (actionOrder) {
            case 1 :
                this.roleSkin.node.active = true;
                this.otherSkin.node.active = false;
                cc.tween(this.node)
                    .delay(1)
                    .call(()=>{
                        this.labList[1].string = tipList[0][0];
                    })
                    .delay(4)
                    .call(()=>{
                        this.labList[1].string = tipList[0][1];
                    })
                    .delay(4)
                    .call(()=>{
                        this.labList[1].string = "";
                        this.roleSkin.node.active = false;
                        this.otherSkin.node.active = true;
                        this.labList[0].string = tipList[1][0];
                    })
                    .delay(4)
                    .call(()=>{
                        this.labList[0].string = tipList[1][1];
                    })
                    .delay(4)
                    .call(()=>{
                        this.labList[0].string = "";
                        this.roleSkin.node.active = true;
                        this.otherSkin.node.active = false;
                        this.labList[1].string = tipList[2][0];
                    })
                    .delay(4)
                    .call(()=>{
                        this.labList[1].string = tipList[2][1];
                    })
                    .delay(4)
                    .call(()=>{
                        this.roleSkin.node.active = false;
                        this.otherSkin.node.active = true;
                        this.labList[1].string = "";
                        this.labList[0].string = tipList[3][0];
                    })
                    .delay(4)
                    .call(()=>{
                        this.labList[0].string = tipList[3][1];
                    })
                    .delay(4)
                    .call(()=>{
                        this.labList[0].string = "";
                        this.node.active = false;
                        callback();
                    })
                    .start();
                    break;
            case 2 :
                this.roleSkin.node.active = true;
                this.otherSkin.node.active = false;
                cc.tween(this.node)
                    .delay(1)
                    .call(()=>{
                        this.labList[1].string = tipList[4][0];
                    })
                    .delay(4)
                    .call(()=>{
                        this.labList[1].string = tipList[4][1];
                    })
                    .delay(4)
                    .call(()=>{
                        this.roleSkin.node.active = false;
                        this.otherSkin.node.active = true;
                        this.labList[1].string = "";
                        this.labList[0].string = tipList[5][0];
                    })
                    .delay(4)
                    .call(()=>{
                        this.labList[0].string = tipList[5][1];
                    })
                    .delay(4)
                    .call(()=>{
                        this.labList[0].string = tipList[5][2];
                    })
                    .delay(4)
                    .call(()=>{
                        this.labList[0].string = "";
                        this.node.active = false;
                        callback();
                    })
                    .start();
                    break;
            case 3 :
                this.roleSkin.node.active = false;
                this.otherSkin.node.active = true;
                cc.tween(this.node)
                    .delay(1)
                    .call(()=>{
                        this.labList[0].string = List1[0];
                    })
                    .delay(4)
                    .call(()=>{
                        this.labList[0].string = List1[1];
                    })
                    .delay(4)
                    .call(()=>{
                        this.labList[0].string = List1[2];
                    })
                    .delay(4)
                    .call(()=>{
                        this.labList[0].string = "";
                        this.node.active = false;
                        callback();
                    }).start();
                break;
            case 4 :
                this.roleSkin.node.active = true;
                this.otherSkin.node.active = false;
                cc.tween(this.node)
                    .delay(1)
                    .call(()=>{
                        this.labList[1].string = List2[0][0];
                    })
                    .delay(4)
                    .call(()=>{
                        this.labList[1].string = List2[0][1];
                    })
                    .delay(4)
                    .call(()=>{
                        this.roleSkin.node.active = false;
                        this.otherSkin.node.active = true;
                        this.labList[1].string = "";
                        this.labList[0].string = List2[1][0];
                    })
                    .delay(4)
                    .call(()=>{
                        this.roleSkin.node.active = true;
                        this.otherSkin.node.active = false;
                        this.labList[0].string = "";
                        this.labList[1].string = List2[2][0];
                    })
                    .delay(4)
                    .call(()=>{
                        this.roleSkin.node.active = false;
                        this.otherSkin.node.active = true;
                        this.labList[1].string = "";
                        this.labList[0].string = List2[3][0];
                    })
                    .delay(4)
                    .call(()=>{
                        this.roleSkin.node.active = true;
                        this.otherSkin.node.active = false;
                        this.labList[1].string = List2[4][0];
                    })
                    .delay(4)
                    .call(()=>{
                        this.labList[1].string = List2[4][1];
                    })
                    .delay(4)
                    .call(()=>{
                        this.roleSkin.node.active = false;
                        this.otherSkin.node.active = true;
                        this.labList[1].string = "";
                        this.labList[0].string = List2[5][0];
                    })
                    .delay(4)
                    .call(()=>{
                        this.labList[0].string = "";
                        this.node.active = false;
                        callback();
                    }).start();
                break;
            case 5 :
                this.roleSkin.node.active = false;
                this.otherSkin.node.active = true;
                cc.tween(this.node)
                    .delay(1)
                    .call(()=>{
                        this.labList[0].string = List3[0][0];
                    })
                    .delay(4)
                    .call(()=>{
                        this.labList[0].string = List3[0][1];
                    })
                    .delay(4)
                    .call(()=>{
                        this.labList[0].string = List3[0][2];
                    })
                    .delay(4)
                    .call(()=>{
                        this.roleSkin.node.active = true;
                        this.otherSkin.node.active = false;
                        this.labList[0].string = "";
                        this.labList[1].string = List3[1][0];
                    })
                    .delay(4)
                    .call(()=>{
                        this.labList[1].string = List3[1][1];
                    })
                    .delay(4)
                    .call(()=>{
                        this.labList[1].string = List3[1][2];
                    })
                    .delay(4)
                    .call(()=>{
                        this.roleSkin.node.active = false;
                        this.otherSkin.node.active = true;
                        this.labList[1].string = "";
                        this.labList[0].string = List3[2][0];
                    })
                    .delay(4)
                    .call(()=>{
                        this.labList[0].string = List3[2][1];
                    })
                    .delay(4)
                    .call(()=>{
                        this.labList[0].string = List3[2][2];
                    })
                    .delay(4)
                    .call(()=>{
                        this.roleSkin.node.active = true;
                        this.otherSkin.node.active = false;
                        this.labList[0].string = "";
                        this.labList[1].string = List3[3][0];
                    })
                    .delay(4)
                    .call(()=>{
                        this.labList[1].string = List3[3][1];
                    })
                    .delay(4)
                    .call(()=>{
                        this.roleSkin.node.active = false;
                        this.otherSkin.node.active = true;
                        this.labList[1].string = "";
                        this.labList[0].string = List3[4][0];
                    })
                    .delay(4)
                    .call(()=>{
                        this.labList[1].string = "";
                        this.node.active = false;
                        callback();
                    }).start();

                break;
            case 6 :
                this.roleSkin.node.active = false;
                this.otherSkin.node.active = true;
                cc.tween(this.node)
                    .delay(1)
                    .call(()=>{
                        this.labList[0].string = List4[0][0];
                    })
                    .delay(4)
                    .call(()=>{
                        this.roleSkin.node.active = true;
                        this.otherSkin.node.active = false;
                        this.labList[0].string = "";
                        this.labList[1].string = List4[1][0];
                    })
                    .delay(4)
                    .call(()=>{
                        this.roleSkin.node.active = false;
                        this.otherSkin.node.active = true;
                        this.labList[1].string = "";
                        this.labList[0].string = List4[2][0];
                    })
                    .delay(4)
                    .call(()=>{
                        this.roleSkin.node.active = true;
                        this.otherSkin.node.active = false;
                        this.labList[0].string = "";
                        this.labList[1].string = List4[3][0];
                    })
                    .delay(4)
                    .call(()=>{
                        this.roleSkin.node.active = false;
                        this.otherSkin.node.active = true;
                        this.labList[1].string = "";
                        this.labList[0].string = List4[4][0];
                    })
                    .delay(4)
                    .call(()=>{
                        this.labList[0].string = List4[4][1];
                    })
                    .delay(4)
                    .call(()=>{
                        this.roleSkin.node.active = true;
                        this.otherSkin.node.active = false;
                        this.labList[0].string = "";
                        this.labList[1].string = List4[5][0];
                    })
                    .delay(4)
                    .call(()=>{
                        this.roleSkin.node.active = true;
                        this.otherSkin.node.active = false;
                        this.labList[0].string = "";
                        this.labList[1].string = List4[5][1];
                    })
                    .delay(4)
                    .call(()=>{
                        this.labList[1].string = "";
                        this.node.active = false;
                        callback();
                    }).start();

                break;
        }
    }

    protected onEnable(): void {
        this.updateConversation(this.curOrder,this.callback);
    }

    setCallBack(order,callback = null){
        this.curOrder = order;
        if(callback == null){
            callback = ()=>{

            }
        }
        this.callback = callback;
    }
    // update (dt) {}
}
